package com.erp.app.common.select.beans;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.erp.app.common.select.dao.SelectDAO;
import com.erp.framework.model.dao.ServiceLocator;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.util.logger.Logger;

public class SelectDelegate extends SimpleSearchMaintStyleDelegate {

    public synchronized ArrayList findDateFields(String table, String column, String where) {
        ArrayList list = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            list = dao.findDateFields(table, column, where, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return list;
    }

    public synchronized ArrayList findFields(String table, String column, String where) {
        ArrayList list = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            list = dao.findFields(table, column, where, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return list;
    }

    public synchronized Object findField(String table, String column, String where) {
        Object obj = new Object();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            obj = dao.findField(table, column, where, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return obj;
    }

    public synchronized ArrayList findFieldList(String table, String[] columns, String where) {
        ArrayList rslt = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rslt = dao.findFieldList(table, columns, where, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }



    public Object selectOneValue(String query) {
        Object obj = new Object();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            obj = dao.selectOneValue(query, conn);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return obj;
    }

    public byte[] selectOneImage(String query) {
        byte[] image = null;
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            image = dao.selectOneImage(query, conn);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return image;
    }
    
    public byte[] selectOneBlobImage(String query) {
        byte[] image = null;
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            image = dao.selectOneBlobImage(query, conn);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return image;
    }

    public ArrayList selectList(String query) {
        ArrayList rslt = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rslt = dao.selectList(query, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }

    public ArrayList selectOneList(String query) {
    	ArrayList rsltList = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rsltList = dao.selectOneList(query, conn);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	System.out.println(e);
            }
        }
        return rsltList;
    }

    public ArrayList selectStringList(String query) {
        ArrayList rslt = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rslt = dao.selectStringList(query, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }

    public Map selectMap(String query) {
        Map rslt = new HashMap();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rslt = dao.selectMap(query, conn);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }

    public synchronized ArrayList findAllMenuIds(String roleGroupId, String companyCode) throws java.sql.SQLException {
        ArrayList rslt = new ArrayList();
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rslt = dao.findAllMenuIds(roleGroupId, companyCode, conn);
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }

    public ArrayList findUserGroupInfo(String compCode, String userGroupCode, String userCode) {
        ArrayList rslt = null;
        Connection conn = null;
        try {
            conn = ServiceLocator.getInstance().getDSDBConnection();
            SelectDAO dao = new SelectDAO();
            rslt = dao.findUserGroupInfo(compCode, userGroupCode, userCode, conn);
        } catch (Exception e) {
            Logger.logExceptions(this, e);
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                Logger.logExceptions(this, e);
            }
        }
        return rslt;
    }

}
